/**
 * Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
 *
 * @Package: com.gitee.mybatis.fl.util
 * @author: flying-cattle
 * @date: 2019年4月9日 下午8:15:25
 */
package com.gitee.flying.cattle.mdg.util;

import Utils.DBUtil;
import com.gitee.flying.cattle.mdg.entity.BeanGenInfo;
import com.gitee.flying.cattle.mdg.entity.PropertyInfo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Copyright: Copyright (c) 2019 
 *
 * <p>说明：  链接数据库并获取表信息</P>
 * @version: v3.0.0
 * @author: flying-cattle
 *
 * Modification History:
 * Date         	Author          Version          Description
 *---------------------------------------------------------------*
 * 2019年4月9日      		flying-cattle   v3.0.0           initialize
 */
public class EntityInfoUtil {
    public static BeanGenInfo getInfo(BeanGenInfo bi) throws SQLException {
        List<PropertyInfo> columns = new ArrayList<PropertyInfo>();
        List<PropertyInfo> keys = new ArrayList<PropertyInfo>();
        // 创建连接
        Connection con = null;
        PreparedStatement pstemt = null;
        ResultSet rs = null;


        //sql
        String sql = "select column_name,data_type,column_comment,COLUMN_KEY from information_schema.columns where table_schema='" + bi.getDatabase() + "' and table_name='" + bi.getTable() + "'";
        try {
            con = DriverManager.getConnection(bi.getDbUrl(), bi.getDbName(), bi.getDbPassword());

            List<String> listKeyFields = DBUtil.getTablePK(bi.getTable(), con);

            if (listKeyFields != null && listKeyFields.size() > 0) {
                bi.setKeyFields(String.join(",", listKeyFields));
            }
            String strKey = "";
            if (listKeyFields != null && listKeyFields.size() > 0) {
                strKey = listKeyFields.get(0);
            }


            if (strKey != "" && strKey != null) {
                bi.setKeyField(strKey);
            }

            pstemt = con.prepareStatement(sql);
            rs = pstemt.executeQuery();
            while (rs.next()) {
                String column = rs.getString(1);
                String jdbcType = rs.getString(2);
                String comment = rs.getString(3);
                PropertyInfo ci = new PropertyInfo();
                ci.setColumn(column);
                if (jdbcType.equalsIgnoreCase("int")) {
                    ci.setJdbcType("Integer");
                } else if (jdbcType.equalsIgnoreCase("datetime")) {
                    ci.setJdbcType("timestamp");
                } else {
                    ci.setJdbcType(jdbcType);
                }
                ci.setComment(comment);
                if (bi.isCapital()) {
                    ci.setProperty(MySqlToJavaUtil.changeToJavaFiled(column));
                } else {
                    ci.setProperty(MySqlToJavaUtil.changeToJavaFiled(column.toLowerCase()));
                }
                ci.setJavaType(MySqlToJavaUtil.jdbcTypeToJavaType(jdbcType));

                //设置注解类型
                if (column.equalsIgnoreCase(strKey)) {
                    bi.setIdType(ci.getJavaType());
                    bi.setIdJdbcType(ci.getJdbcType());
                }

                //多个主键
                if (listKeyFields.contains(column)) {
                    ci.setIsKey(true);
                    keys.add(ci);


                }
                columns.add(ci);
                //添加包路径
                Set<String> pkgs = bi.getPkgs();
                pkgs.add(MySqlToJavaUtil.jdbcTypeToJavaTypePck(jdbcType));
                bi.setPkgs(pkgs);
            }
            bi.setKeys(keys);
            bi.setCis(columns);
            // 完成后关闭
            rs.close();
            pstemt.close();
            con.close();
            if (null == columns || columns.size() == 0) {
                /*				throw new RuntimeException("未能读取到表或表中的字段。请检查链接url，数据库账户，数据库密码，查询的数据名、是否正确。");*/
                return null;
            }
            //去处主键
            //	for (int i=0;i<bi.getKeys().size();i++)
            //	{
            //	PropertyInfo item=	bi.getKeys().get(0);

            for (PropertyInfo propertyInfo : bi.getCis()) {

//					if(item2.getColumn().equalsIgnoreCase(item.getColumn()))
//					{
//						item2.setIsKey(true);
//					}
                if (!propertyInfo.getIsKey()) {
                    bi.getColumsExceptKeys().add(propertyInfo);
                }
            }
            //	}
            return bi;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("自动生成实体类错误：" + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
            } catch (SQLException se2) {
            }
            // 关闭资源
            try {
                if (pstemt != null) pstemt.close();
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (con != null) con.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }
}
